#include "findtextdialog.h"
#include "ui_findtextdialog.h"
#include <QButtonGroup>
#include <QDebug>

FindTextDialog::FindTextDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FindTextDialog)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);
    QButtonGroup *buttonGroup = new QButtonGroup(this);
    buttonGroup->addButton(ui->upRadioBtn);
    buttonGroup->addButton(ui->downRadioBtn);
    buttonGroup->setExclusive(true); // 确保互斥
    ui->groupBox->setTitle("方向:");

    //设置默认查找方向为“向上”
    ui->upRadioBtn->setChecked(true);
    //设置循环查找
    ui->loop->setCheckState(Qt::Checked);

    ui->findLineEdit->setMaxLength(20);
    ui->findNextBtn->setEnabled(false);
    connect(ui->findLineEdit,&QLineEdit::textChanged,this,&FindTextDialog::setNextBtnEnable);

}

FindTextDialog::~FindTextDialog()
{
    delete ui;
}

void FindTextDialog::on_closeBtn_clicked()
{
    this->close();
}

void FindTextDialog::on_cancelBtn_clicked()
{
    this->close();
}

void FindTextDialog::setNextBtnEnable()
{
    ui->findNextBtn->setEnabled(!ui->findLineEdit->text().isEmpty());
}

void FindTextDialog::on_findNextBtn_clicked()
{
    if(ui->findLineEdit->text().isEmpty())
    {
        return;
    }

    if(ui->upRadioBtn->isChecked())
    {
        direction = true;
    }else
    {
        direction = false;
    }
    emit searchText(ui->findLineEdit->text(),ui->caseSensitive->isChecked(),ui->loop->isChecked(),direction);
}

void FindTextDialog::mousePressEvent(QMouseEvent *e)
{

    if(e->button() == Qt::LeftButton &&ui->frame->geometry().contains(e->pos()))
    {
        m_dragPos = e->globalPos() - frameGeometry().topLeft();
        e->accept();
    }
}

void FindTextDialog::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() & Qt::LeftButton)
    {
        move(e->globalPos()-m_dragPos);
        e->accept();
    }
}
